using System;
using System.Collections.Generic;
using System.Text;
using LngT.Interfaces.UserAccess;
using LngT.Interfaces.UserRoles;

namespace LngT.User
    {
    [Serializable]
    public class UserManager
        {
        private List<User> _users = new List<User>();
        private UserManager()
            {
            }

        private static UserManager _instance;
        public static UserManager Instance
            {
            get
                {
                if( _instance == null )
                    _instance = new UserManager();
                return _instance;
                }
            }

        public IUserRole GetUserPrivileges( string userName, string password )
            {
            foreach( User usr in _users )
                if( usr.UserName == userName && usr.Password == password )
                    return usr.Privileges;
            return null;
            }

        public void UpdateUserPrivileges( string userName, IUserRole privileges )
            {
            foreach( User usr in _users )
                {
                if( usr.UserName == userName )
                    usr.Privileges = privileges;
                }
            }

        public void AddUser( string userName, string password, IUserRole privileges )
            {
            if( password == null || password == "" )
                throw new ArgumentNullException( "Password cannot be null or empty!" );
            if( userName == null || userName == "" )
                throw new ArgumentNullException( "Username cannot be null or empty!" );
            
            foreach( User usr in _users )
                {
                if( usr.UserName == userName )
                    throw new ArgumentException( "User name already exists!" );
                }
            _users.Add( new User( userName, password, privileges ));
            }

        public bool RemoveUser( string userName )
            {
            User usrToRemove = null;
            foreach( User usr in _users )
                {
                if( usr.UserName == userName )
                    usrToRemove = usr;
                }
            if( usrToRemove != null )
                {
                _users.Remove( usrToRemove );
                return true;
                }
            return false;
            }

        public List<string> GetUserList()
            {
            List<string> result = new List<string>();
            foreach( User usr in _users )
                result.Add( usr.UserName );
            return result;
            }

        }
    }
